package com.yueshang.neuron.modular.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yueshang.common.entity.ResultResponse;
import com.yueshang.common.support.BeanKit;
import com.yueshang.framework.constant.factory.PageFactory;
import com.yueshang.neuron.common.enums.ResultCodeEnum;
import com.yueshang.neuron.common.util.InfiniteLevelTreeUtil;
import com.yueshang.neuron.common.util.Node;
import com.yueshang.neuron.core.factory.ConstantFactory;
import com.yueshang.neuron.core.util.InstanceUtil;
import com.yueshang.neuron.core.warpper.RoleWarpper;
import com.yueshang.neuron.core.warpper.UserWarpper;
import com.yueshang.neuron.modular.system.api.IDeptService;
import com.yueshang.neuron.modular.system.api.IDictService;
import com.yueshang.neuron.modular.system.api.IRoleService;
import com.yueshang.neuron.modular.system.model.Dept;
import com.yueshang.neuron.modular.system.model.Dict;
import com.yueshang.neuron.modular.system.model.Role;
import com.yueshang.neuron.modular.system.model.User;
import com.yueshang.neuron.modular.system.vo.res.node.ZTreeNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 岗位管理入口
 *
 * @auton shililu
 * @data 2018-08-30
 */
@RestController
@RequestMapping("/v1/role")
public class RoleController extends ApiController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IDictService dictService;

    @GetMapping("/roleType")
    public ResultResponse getRoleType() {
        Map<String, Object> map = InstanceUtil.newHashMap();
        map.put("roleTypes", dictService.selectSelectByParentCode("station_flag"));
        return ResultResponse.ok(ResultCodeEnum.GAIN_SUCCESS.getCode(), ResultCodeEnum.GAIN_SUCCESS.getMsg(), map);
    }

    /**
     * 添加岗位
     *
     * @param role
     * @return
     */
    @PostMapping
    public ResultResponse addUser(@RequestBody Role role) {
        roleService.save(role);
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode());
    }

    /**
     * 更新岗位
     *
     * @param role
     * @return
     */
    @PutMapping
    public ResultResponse editUser(@RequestBody Role role) {
        roleService.updateById(role);
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode());
    }

    /**
     * 删除岗位
     *
     * @param id 删除部门通过id
     * @return
     */
    @DeleteMapping
    public ResultResponse delUser(Long id) {
        roleService.removeById(id);
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode());
    }

    /**
     * 分页获取岗位列表
     */
    @GetMapping("/roles")
    public Object list(@RequestParam(required = false) String searchCondition) {
        Page<Role> page = new PageFactory<Role>().defaultPage();
        logger.info("UserController-getUserPageList--page:{}", page.getPages());
        IPage<Map<String, Object>> rolePages = roleService.pageMaps(page, StringUtils.isNotBlank(searchCondition) ? new QueryWrapper<Role>().like("simplename", searchCondition) : null);
        page.setRecords((List<Role>) new RoleWarpper(rolePages.getRecords()).warp());
        return ResultResponse.ok(ResultCodeEnum.GAIN_SUCCESS.getCode(), ResultCodeEnum.GAIN_SUCCESS.getMsg(), page);
    }

    /**
     * 分页获取用户列表
     */
    @GetMapping("/tree")
    public ResultResponse roleTreeList() {

        List<Node> nodeList = roleService.getNodeList("");
        List<Node> reNodeList = InfiniteLevelTreeUtil.getInfiniteLevelTree(nodeList);
        return ResultResponse.ok(ResultCodeEnum.GAIN_SUCCESS.getCode(), ResultCodeEnum.GAIN_SUCCESS.getMsg(), reNodeList);
    }

}
